home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 176-200 / 190 / nethack / een.zoo / pcunix.c < prev    next >
C/C++ Source or Header  |  1988-07-01  |  4KB  |  150 lines

  1. /*    SCCS Id: @(#)pcunix.c    1.4    87/08/08
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* unix.c - version 1.0.3 */
  4.  
  5. /* This file collects some Unix dependencies; pager.c contains some more */
  6.  
  7. /*
  8.  * The time is used for:
  9.  *    - seed for rand()
  10.  *    - year on tombstone and yymmdd in record file
  11.  *    - phase of the moon (various monsters react to NEW_MOON or FULL_MOON)
  12.  *    - night and midnight (the undead are dangerous at midnight)
  13.  *    - determination of what files are "very old"
  14.  */
  15.  
  16. #include <stdio.h>    /* mainly for NULL */
  17. #include "hack.h"    /* mainly for index() which depends on BSD */
  18.  
  19. #ifndef __TURBOC__  /* Turbo C has time_t in time.h */
  20. #include    <sys/types.h>     /* for time_t */
  21. #endif
  22. #include    <time.h>
  23. #include    <sys/stat.h>
  24.  
  25. extern time_t time();
  26. static struct stat buf, hbuf;
  27.  
  28. setrandom()
  29. {
  30.     (void) srand((int) time ((time_t *) 0));
  31. }
  32.  
  33. struct tm *
  34. getlt()
  35. {
  36.     time_t date;
  37.     struct tm *localtime();
  38.  
  39.     (void) time(&date);
  40.     return(localtime(&date));
  41. }
  42.  
  43. getyear()
  44. {
  45.     return(1900 + getlt()->tm_year);
  46. }
  47.  
  48. char *
  49. getdate()
  50. {
  51.     static char datestr[7];
  52.     register struct tm *lt = getlt();
  53.  
  54.     (void) sprintf(datestr, "%2d%2d%2d",
  55.     lt->tm_year, lt->tm_mon + 1, lt->tm_mday);
  56.     if(datestr[2] == ' ') datestr[2] = '0';
  57.     if(datestr[4] == ' ') datestr[4] = '0';
  58.     return(datestr);
  59. }
  60.  
  61. phase_of_the_moon()            /* 0-7, with 0: new, 4: full */
  62. {             /* moon period: 29.5306 days */
  63.             /* year: 365.2422 days */
  64.     register struct tm *lt = getlt();
  65.     register int epact, diy, golden;
  66.  
  67.     diy = lt->tm_yday;
  68.     golden = (lt->tm_year % 19) + 1;
  69.     epact = (11 * golden + 18) % 30;
  70.     if ((epact == 25 && golden > 11) || epact == 24)
  71.     epact++;
  72.  
  73.     return( (((((diy + epact) * 6) + 11) % 177) / 22) & 7 );
  74. }
  75.  
  76. night()
  77. {
  78.     register int hour = getlt()->tm_hour;
  79.  
  80.     return(hour < 6 || hour > 21);
  81. }
  82.  
  83. midnight()
  84. {
  85.     return(getlt()->tm_hour == 0);
  86. }
  87.  
  88. gethdate(name) char *name; {
  89. /* old version - for people short of space */
  90. /*
  91. /* register char *np;
  92. /*    if(stat(name, &hbuf))
  93. /*        error("Cannot get status of %s.",
  94. /*            (np = rindex(name, '/')) ? np+1 : name);
  95. /*
  96. /* version using PATH from: seismo!gregc@ucsf-cgl.ARPA (Greg Couch) */
  97.  
  98. /*
  99.  * The problem with   #include    <sys/param.h> is that this include file
  100.  * does not exist on all systems, and moreover, that it sometimes includes
  101.  * <sys/types.h> again, so that the compiler sees these typedefs twice.
  102.  */
  103. #define     MAXPATHLEN    1024
  104.  
  105. register char *np, *path;
  106. char filename[MAXPATHLEN+1];
  107.  
  108.     if (index(name, '/') != NULL || (path = getenv("PATH")) == NULL)
  109.     path = "";
  110.  
  111.     for (;;) {
  112.     if ((np = index(path, ':')) == NULL)
  113.         np = path + strlen(path);       /* point to end str */
  114.     if (np - path <= 1)                     /* %% */
  115.         (void) strcpy(filename, name);
  116.     else {
  117.         (void) strncpy(filename, path, np - path);
  118.         filename[np - path] = '/';
  119.         (void) strcpy(filename + (np - path) + 1, name);
  120.     }
  121.     if (stat(filename, &hbuf) == 0)
  122.         return;
  123.     if (*np == '\0')
  124.     path = "";
  125.     path = np + 1;
  126.     }
  127.     error("Cannot get status of %s.", (np = rindex(name, '/')) ? np+1 : name);
  128. }
  129.  
  130. uptodate(fd) {
  131.     if(fstat(fd, &buf)) {
  132.     pline("Cannot get status of saved level? ");
  133.     return(0);
  134.     }
  135.     if(buf.st_mtime < hbuf.st_mtime) {
  136.     pline("Saved level is out of date. ");
  137.     return(0);
  138.     }
  139.     return(1);
  140. }
  141.  
  142. regularize(s)    /* normalize file name - we don't like ..'s or /'s */
  143. register char *s;
  144. {
  145.     register char *lp;
  146.  
  147.     while((lp = index(s, '.')) || (lp = index(s, '/')))
  148.     *lp = '_';
  149. }
  150.